﻿<%@ WebHandler Language="C#" Class="ZSTestRunReturnBillHandler" %>

using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using Newtonsoft.Json;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;
/// <summary>
/// 试车返回单业务
/// add by longb 2022-06-17
/// </summary>

public class ZSTestRunReturnBillHandler : IHttpHandler, IRequiresSessionState
{

    CurrentUserInfo m_UserInfo;
    TestRunBillBusiness returnbill = new TestRunBillBusiness();
    uLMSCommonIBusiness m_Common = uMESBusinessFactory.instance.GetuLMSCommonObject();
    #region 页面初始化
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            switch (action)
            {
                case "SaveReturnBillData"://保存试车返回单 
                    SaveNoticeBillData(context);
                    break;
                case "SubmitReturnBillData"://提交审核试车返回单
                    SubmitReturnBillData(context);
                    break;
                case "DeleteReturnBillData"://删除试车返回单
                    DeleteReturnBillData(context);
                    break;
                case "ConfirmReturnBillData"://审核试车返回单
                    ConfirmReturnBillData(context);
                    break;
                case "ReturnBillData"://退回试车返回单
                    ReturnReturnBillData(context);
                    break;
                case "RecieveReturnBillData"://接收试车返回单
                    RecieveReturnBillData(context);
                    break;
                case "FinishReturnBillData":
                    FinishReturnBillData(context);
                    break;
                case "GetReturnBillData"://查询试车返回单
                    GetReturnBillData(context);
                    break;
                case "GetReturnBillDataByID":
                    GetReturnBillDataByID(context);
                    break;
                case "GetTRReturnCFBill"://查询成附件
                    GetTRReturnCFBill(context);
                    break;
                case "InsertTRReturnCFBill"://添加成附件
                    InsertTRReturnCFBill(context);
                    break;
                case "DeleteTRReturnCFBillData"://删除成附件
                    DeleteTRReturnCFBillData(context);
                    break;   
                case "GetTRReturnUFBillData"://查询未排除的故障信息
                    GetTRReturnUFBillData(context);
                    break;
                case "DaleteTRReturnUFBillData"://删除未排除的故障信息
                    DaleteTRReturnUFBillData(context);
                    break;
                case "SaveTRReturnUFBillData"://删除未排除的故障信息
                    SaveTRReturnUFBillData(context);
                    break;        
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    #endregion

    #region 方法
    /// <summary>
    /// 保存试车返回单 
    /// </summary>
    /// <param name="context"></param>
    protected void SaveNoticeBillData(HttpContext context) 
    {
        uMESProcessResult result = new uMESProcessResult();
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var ZSTESTRUNRETURNBILLID = dt.Rows[0]["ZSTESTRUNRETURNBILLID"].ToString();

        var strSequence = "000000" + returnbill.GetReturnBillCount();
        var noticeBillNo = "SCF-" + DateTime.Now.ToString("yyyy") + "-" + m_UserInfo.FactoryCode + "-" + strSequence.Substring(strSequence.Length - 4);
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNRETURNBILLID", string.IsNullOrWhiteSpace(ZSTESTRUNRETURNBILLID) ? Guid.NewGuid().ToString() : ZSTESTRUNRETURNBILLID);//唯一ID
        para.Add("ZSTESTRUNRETURNBILLNO", noticeBillNo);
        para.Add("MFGORDERID", dt.Rows[0]["MfgorderID"].ToString());//订单
        para.Add("TESTFINISHDATE", dt.Rows[0]["TESTFINISHDATE"].ToString());//装配完成日期
        para.Add("DUTYWORKCENTERID", dt.Rows[0]["DutyWorkcenterID"].ToString());//责任中心
        para.Add("TESTBED", dt.Rows[0]["TestBed"].ToString());//试车台
        para.Add("TESTRUNTYPE", dt.Rows[0]["TestRunType"].ToString());//试车类型
        para.Add("TURBINENO", dt.Rows[0]["TURBINENO"].ToString());//燃气涡轮启动机号
        para.Add("GREASEEXPEND", dt.Rows[0]["GREASEEXPEND"].ToString());//滑油消耗量L/H
        para.Add("DYTCGX", dt.Rows[0]["DYTCGX"].ToString());//低压停车惯性时间(秒)
        para.Add("DYTCQD", dt.Rows[0]["DYTCQD"].ToString());//低压停车启动次数
        para.Add("DYTCJQD", dt.Rows[0]["DYTCJQD"].ToString());//低压停车假启动动次数
        para.Add("GYTCGX", dt.Rows[0]["GYTCGX"].ToString());//高压停车惯性时间(秒)
        para.Add("GYTCYZ", dt.Rows[0]["GYTCYZ"].ToString());//高压停车运转次数
        para.Add("GYTCJYF", dt.Rows[0]["GYTCJYF"].ToString());//高压停车油封次数
        para.Add("TOTALWORKMINUTES", dt.Rows[0]["TOTALWORKMINUTES"].ToString());//总工作时间(分钟)
        para.Add("WORKMAXLARGESTATE", dt.Rows[0]["WORKMAXLARGESTATE"].ToString());//最大状态工作时间(分钟)
        para.Add("MAXLARGET", dt.Rows[0]["MAXLARGET"].ToString());//最大状态-T5hs值
        para.Add("MAXLARGEF", dt.Rows[0]["MAXLARGEF"].ToString());//最大状态-Fhs值
        para.Add("MAXLARGEVIBRATE", dt.Rows[0]["MAXLARGEVIBRATE"].ToString());//最大状态振动 值
        para.Add("CREATEEMPLOYEEID", m_UserInfo.EmployeeID.ToString());//创建人
        para.Add("BILLTYPE", "2");
        var count = string.IsNullOrWhiteSpace(ZSTESTRUNRETURNBILLID) ? returnbill.InsertReturnBillData(para) : returnbill.UpdateReturnBillData(para);
        if (count > 0)
        {
            result.Result = true;
            result.Message = "保存成功";
        }
        else
        {
            result.Message = "保存失败";
        }
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 提交审核试车返回单
    /// </summary>
    /// <param name="context"></param>
    protected void SubmitReturnBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTestRunNoticeBillID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNRETURNBILLID", ZSTestRunNoticeBillID);
        para.Add("STATUS", "1");
        result.Result=returnbill.UpdateReturnBillData(para) > 0;
        result.Message =result.Result?"提交成功":"提交失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 删除试车返回单
    /// </summary>
    /// <param name="context"></param>
    protected void DeleteReturnBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTestRunNoticeBillID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNRETURNBILLID", ZSTestRunNoticeBillID);
        result.Result = returnbill.DeleteReturnBillData(para) > 0;
        result.Message =result.Result? "删除成功":"删除失败";   
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 审核试车返回单
    /// </summary>
    /// <param name="context"></param>
    protected void ConfirmReturnBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTestRunNoticeBillID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNRETURNBILLID", ZSTestRunNoticeBillID);
        para.Add("AUDITEMPLOYEEID", m_UserInfo.EmployeeID.ToString());
        result.Result=returnbill.UpdateReturnBillData(para) > 0;
        result.Message =result.Result? "审核成功":"审核失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 退回试车返回单
    /// </summary>
    /// <param name="context"></param>
    protected void ReturnReturnBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTESTRUNRETURNBILLID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNRETURNBILLID", ZSTESTRUNRETURNBILLID);
        para.Add("STATUS", "0");
        result.Result=returnbill.UpdateReturnBillData(para) > 0;
        result.Message = result.Result?"退回成功":"退回失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 接收试车返回单
    /// </summary>
    /// <param name="context"></param>
    protected void RecieveReturnBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTESTRUNRETURNBILLID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNRETURNBILLID", ZSTESTRUNRETURNBILLID);
        para.Add("EXCLUDEMETHODS", "1");
        var dt=returnbill.GetTRReturnUFBillData(para);
        if (dt.Rows.Count > 0) {
            result.Message = "所选试车返回单在【试车未排出的故障】中存在未填写排除方法的记录,不能进行接收";
        }
        else
        {
            para["EXCLUDEMETHODS"]="";
            para.Add("RECIEVEEMPLOYEEID", m_UserInfo.EmployeeID.ToString());
            result.Result = returnbill.UpdateReturnBillData(para) > 0;
            result.Message = result.Result ? "接收成功" : "接收失败";
        }
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 完成返回单信息
    /// </summary>
    /// <param name="context"></param>
    protected void FinishReturnBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTESTRUNRETURNBILLID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNRETURNBILLID", ZSTESTRUNRETURNBILLID);
        para.Add("STATUS","15");
        result.Result=returnbill.UpdateReturnBillData(para) > 0;
        result.Message =result.Result? "操作成功":"操作失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 查询试车返回单
    /// </summary>
    /// <param name="context"></param>
    protected void GetReturnBillData(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        string pageSize = context.Request["pageSize"].ToString();
        string pageNumber = context.Request["pageNumber"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("NoticeBillNo", dt.Rows[0]["ReturnBillNo"].ToString());
        para.Add("Machine", dt.Rows[0]["Machine"].ToString());
        para.Add("MfgOrdername", dt.Rows[0]["MfgOrdername"].ToString());
        para.Add("Status", dt.Rows[0]["Status"].ToString());
        para.Add("startedate", dt.Rows[0]["startedate"].ToString());
        para.Add("enddate", dt.Rows[0]["enddate"].ToString());
        para.Add("ZSTESTRUNRETURNBILLID", dt.Rows[0]["ZSTESTRUNRETURNBILLID"].ToString());
        para.Add("PageSize", pageSize);
        para.Add("CurrentPageIndex", pageNumber);
        uMESProcessResult result = returnbill.GetReturnBillData(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    protected void GetReturnBillDataByID(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        string pageSize = context.Request["pageSize"].ToString();
        string pageNumber = context.Request["pageNumber"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNRETURNBILLID", dt.Rows[0]["ZSTESTRUNRETURNBILLID"].ToString());
        para.Add("PageSize", pageSize);
        para.Add("CurrentPageIndex", pageNumber);
        uMESProcessResult result = returnbill.GetReturnBillData(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    
    /// <summary>
    /// 查询成附件
    /// </summary>
    /// <param name="context"></param>
    protected void GetTRReturnCFBill(HttpContext context) {
        uMESProcessResult result = new uMESProcessResult();
        var ZSTESTRUNRETURNBILLID = context.Request["ZSTESTRUNRETURNBILLID"].ToString();
        result.DBTable = m_Common.GetTableInfo("ZSTRReturnCFBill", "ZSTESTRUNRETURNBILLID", ZSTESTRUNRETURNBILLID);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 删除成附件
    /// </summary>
    /// <param name="context"></param>
    protected void DeleteTRReturnCFBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTRETURNCFBILLID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTRETURNCFBILLID", ZSTRETURNCFBILLID);
        result.Result = returnbill.DeleteTRReturnCFBillData(para) > 0;
        result.Message=result.Result?"删除成功":"删除失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 添加成附件
    /// </summary>
    /// <param name="context"></param>
    protected void InsertTRReturnCFBill(HttpContext context) {
        uMESProcessResult result = new uMESProcessResult();
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        List<ExcuteEntity> List = new List<ExcuteEntity>();
        var InsertEntity = new ExcuteEntity("ZSTRReturnCFBill", ExcuteEntity.ExcuteTypeEnum.insert);
        List<FieldEntity> InsertList = new List<FieldEntity>();
        InsertList.Add(new FieldEntity("ZSTRETURNCFBILLID", Guid.NewGuid().ToString(), FieldEntity.FieldTypeEnum.Str));
        InsertList.Add(new FieldEntity("ZSTESTRUNRETURNBILLID", dt.Rows[0]["ZSTESTRUNRETURNBILLID"].ToString(), FieldEntity.FieldTypeEnum.Str));
        InsertList.Add(new FieldEntity("DEPENDTYPE", dt.Rows[0]["DEPENDTYPE"].ToString(), FieldEntity.FieldTypeEnum.Str));
        InsertList.Add(new FieldEntity("PREBATCHNO", dt.Rows[0]["PREBATCHNO"].ToString(), FieldEntity.FieldTypeEnum.Str));
        InsertList.Add(new FieldEntity("AFTERBATCHNO", dt.Rows[0]["AFTERBATCHNO"].ToString(), FieldEntity.FieldTypeEnum.Str));
        InsertList.Add(new FieldEntity("PREWORKMINS", dt.Rows[0]["PREWORKMINS"].ToString(), FieldEntity.FieldTypeEnum.Str));
        InsertList.Add(new FieldEntity("CHANGEREASON", dt.Rows[0]["CHANGEREASON"].ToString(), FieldEntity.FieldTypeEnum.Str));
        InsertEntity.ExcuteFileds = InsertList;
        List.Add(InsertEntity);
        result.Result = DatabaseAccess.DBUtility.DatabaseHelper.ExecuteDataByEntitys(List);
        result.Message=result.Result?"添加成功":"添加失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 查询未排除故障信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetTRReturnUFBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        var ZSTESTRUNRETURNBILLID = context.Request["ZSTESTRUNRETURNBILLID"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNRETURNBILLID", ZSTESTRUNRETURNBILLID);
        result.DBTable = returnbill.GetTRReturnUFBillData(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 删除未排除故障信息
    /// </summary>
    /// <param name="context"></param>
    protected void DaleteTRReturnUFBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTRRETURNUFBILLID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTRRETURNUFBILLID", ZSTRRETURNUFBILLID);
        result.Result = returnbill.DeleteTRReturnUFBillData(para) > 0;
        result.Message = result.Result ? "删除成功" : "删除失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 保存未排除故障信息
    /// </summary>
    /// <param name="context"></param>
    protected void SaveTRReturnUFBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        List<ExcuteEntity> List = new List<ExcuteEntity>();
        if (string.IsNullOrEmpty(dt.Rows[0]["ZSTRRETURNUFBILLID"].ToString()))
        {
            var InsertEntity = new ExcuteEntity("ZSTRReturnUFBill", ExcuteEntity.ExcuteTypeEnum.insert);
            List<FieldEntity> InsertList = new List<FieldEntity>();
            InsertList.Add(new FieldEntity("ZSTRRETURNUFBILLID", Guid.NewGuid().ToString(), FieldEntity.FieldTypeEnum.Str));
            InsertList.Add(new FieldEntity("ZSTESTRUNRETURNBILLID", dt.Rows[0]["ZSTESTRUNRETURNBILLID"].ToString(), FieldEntity.FieldTypeEnum.Str));
            InsertList.Add(new FieldEntity("SERIALNUMBER", dt.Rows[0]["SERIALNUMBER"].ToString(), FieldEntity.FieldTypeEnum.Str));
            InsertList.Add(new FieldEntity("FAULTTYPE", dt.Rows[0]["FAULTTYPE"].ToString(), FieldEntity.FieldTypeEnum.Str));
            InsertList.Add(new FieldEntity("FAULTPERFORM", dt.Rows[0]["FAULTPERFORM"].ToString(), FieldEntity.FieldTypeEnum.Str));
            InsertList.Add(new FieldEntity("CREATEDATE", DateTime.Now, FieldEntity.FieldTypeEnum.DateTime));
            InsertList.Add(new FieldEntity("CREATEEMPLOYEEID", m_UserInfo.EmployeeID.ToString(), FieldEntity.FieldTypeEnum.Str));
            InsertEntity.ExcuteFileds = InsertList;
            List.Add(InsertEntity);
        }
        else {
            var UpdateEntity = new ExcuteEntity("ZSTRReturnUFBill", ExcuteEntity.ExcuteTypeEnum.update);
            List<FieldEntity> updateList = new List<FieldEntity>();
            List<FieldEntity> whereList = new List<FieldEntity>();
            whereList.Add(new FieldEntity("ZSTRRETURNUFBILLID", dt.Rows[0]["ZSTRRETURNUFBILLID"].ToString(), FieldEntity.FieldTypeEnum.Str));
            updateList.Add(new FieldEntity("EXCLUDEMETHODS", dt.Rows[0]["EXCLUDEMETHODS"].ToString(), FieldEntity.FieldTypeEnum.Str));
            updateList.Add(new FieldEntity("EXCLUDEDATE", DateTime.Now, FieldEntity.FieldTypeEnum.DateTime));
            updateList.Add(new FieldEntity("EXCLUDEEMPLOYEEID", m_UserInfo.EmployeeID.ToString(), FieldEntity.FieldTypeEnum.Str));
            UpdateEntity.ExcuteFileds = updateList;
            UpdateEntity.WhereFileds = whereList;
            List.Add(UpdateEntity);
        }
        var Str = string.IsNullOrEmpty(dt.Rows[0]["ZSTRRETURNUFBILLID"].ToString()) ? "添加" : "保存";
        result.Result = DatabaseAccess.DBUtility.DatabaseHelper.ExecuteDataByEntitys(List);
        result.Message = result.Result ? Str+"成功" :Str+"失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    
    #endregion
}